package com.example.myprojectoffeixue;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import org.apache.http.entity.StringEntity;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

public class SbXingHaoActivity extends Activity {

    private ListView lv;

    private ArrayList<HongWaiSB> parseArray;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sb_xing_hao);

    }

    @Override
    protected void onStart() {
        info();
        super.onStart();
    }

    private void info() {
        MyApplication m = (MyApplication) getApplication();
        lv = (ListView) findViewById(R.id.lv_sb_xinghao);

        Intent it = getIntent();

        HongWaiSB h = new HongWaiSB();
        h.kind = m.type;
        String j = JSON.toJSONString(h);
        j = AESUtil.encrypt(j);
        HttpUtils httpUtils = new HttpUtils();
        RequestParams params = new RequestParams();
        params.setHeader("Content-Type", "application/json");
        try {
            params.setBodyEntity(new StringEntity(j, "utf-8"));
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        httpUtils.send(HttpMethod.POST, "http://" + UserClass.ServerIP + "/FXMobiServer/remote/getCompanyList", params,
                new RequestCallBack<String>() {


                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                        HttpConnection.showToast(getBaseContext(),"网络异常");
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> arg0) {
                        if(arg0.result != null && !("".equals(arg0.result))){

                        arg0.result = AESUtil.decrypt(arg0.result);
                        JSONObject j = JSONObject.parseObject(arg0.result);
                        String status=j.getString("status");
                        if("0".equals(status)){
                        JSONArray j1 = j.getJSONArray("companyList");
                        parseArray = (ArrayList<HongWaiSB>) JSONArray.parseArray(j1.toString(), HongWaiSB.class);
                        HWadapter h = new HWadapter(parseArray, SbXingHaoActivity.this, 2);
                        lv.setAdapter(h);

                    }else{

                      HttpConnection.showToast(SbXingHaoActivity.this,"服务器错误");

                    }}else{
                            HttpConnection.showToast(SbXingHaoActivity.this,"服务器错误");
                    }}
                });

        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


                Intent it = new Intent(SbXingHaoActivity.this, JuTixinghaoActivity.class);
                it.putExtra("id", parseArray.get(position).id);
                startActivity(it);

            }


        });


    }


}
